package behavior.Observer;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by LYJ on 2016/10/21.
 */
public abstract class Subject2{
    private List<Observer2> list = new ArrayList<>();

    public void attach(Observer2 observer)
    {
        list.add(observer);
    }

    public void detach(Observer2 observer)
    {
        list.remove(observer);
    }

    public void notifyObservers()
    {
        /*
        int len = list.size();
        for(int i=0;i<len;i++)
        {
            list.get(i).update(this);
        }
        */
        //上面的代码可以用lambda表达式来写
        list.forEach(observer2 -> observer2.update(this));
    }
}
